//
//  FPCommodityDetailVC.m
//  ForestPack
//
//  Created by jimmy on 2018/7/10.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FPCommodityDetailVC.h"
#import "FPCommodityDetailCell.h"
#import "FPCommodityHeaderView.h"
#import "CommentProgressCell.h"
#import "HMsegmentedControl.h"
#import "Masonry.h"
#import "UIView+Utils.h"
#import "FPBottomButtonView.h"
#import "EvaluationDetailVC.h"
#import "FPPublicConmmandVC.h"
#import "ShopAdornViewController.h"
#import "CommodityDetailModel.h"
#import "CommentShowTool.h"

@interface FPCommodityDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView       * tableView;
@property (nonatomic,strong)  HMSegmentedControl  * segControl;
///服务详情
@property (nonatomic,strong)  CommodityDetailModel  * commodityDetailModel;
@property (nonatomic,strong)  NSMutableArray        * commodityDetailArray;

@property (nonatomic,strong)  NSString                 * webContent;
@property (nonatomic,assign)  CGFloat                    webHeight;

@property (nonatomic,strong)  NSMutableArray  * commentArray;

@end

@implementation FPCommodityDetailVC

- (NSMutableArray *)commentArray
{
    if (_commentArray == nil) {
        _commentArray = [[NSMutableArray alloc] init];
    }
    return _commentArray;
}

- (NSMutableArray *)commodityDetailArray
{
    if (_commodityDetailArray == nil) {
        _commodityDetailArray = [[NSMutableArray alloc] init];
    }
    return _commodityDetailArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setNavViewUI];
    [self setTableViewUI];
    [self setBottomViewUI];
    
    [self requestCaseDetailData];
}

#pragma mark - 请求案例详情接口数据
-(void)requestCaseDetailData
{
    NSString * url = [NSString stringWithFormat:@"%@app/service_detail",REQUESTHEADER];
    NSDictionary * parameter = @{@"id":@(self.design_id)};
    [HttpManager showWait];
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
        [HttpManager hideWait];
        [selfWeak.tableView.mj_header endRefreshing];
        NSDictionary *dict = successResponse;
        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSError *error;
            // 成功
            selfWeak.commodityDetailModel = [[CommodityDetailModel alloc] initWithDictionary:dict error:&error];
            
            if (selfWeak.commodityDetailModel != nil) {
                [selfWeak.commodityDetailArray addObject:self.commodityDetailModel];
                [selfWeak.tableView reloadData];
            }
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [selfWeak.tableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}

#pragma mark - 评价详情的接口
- (void)requestRecommentData
{
    [CommentShowTool requestCommentShowDataWithGoods_id:self.design_id withType:9 success:^(CommentShowModel *commentShowModel) {
        [self.commentArray addObject:commentShowModel];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    }];
}


-(void)segmentDidChangedValue
{
    if (self.segControl.selectedSegmentIndex == 0) {
        [self.tableView setContentOffset:CGPointMake(0  , 0) animated:true];

    }else if(self.segControl.selectedSegmentIndex == 1){
        [self.tableView setContentOffset:CGPointMake(0  , 476) animated:true];
        
    }else{
        //这里需要判断一下，如果详情里的东西距离比屏幕小，就点击后就详情底部在bottomView上方
        //如果详情页里的东西比屏幕大，就点击后跳到详情顶部距离：（这里还要根据评论的）
        CGFloat DetailHeight = 476 + 550;
        [self.tableView setContentOffset:CGPointMake(0  , DetailHeight) animated:true];
        
    }
    
}

-(void)moreClick
{
    
}

-(void)seeMoreClick
{
    [HttpManager showNoteMsg:@"暂无更多"];
//    EvaluationDetailVC *evaluateVC = [[EvaluationDetailVC alloc] init];
//    evaluateVC.design_id = self.commodityDetailModel.data.Id;
//    [self.navigationController pushViewController:evaluateVC animated:true];
}

-(void)setNavViewUI
{
    self.segControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"服务",@"评价",@"详情"]];
    self.segControl.frame = CGRectMake(0, 7, ScreenX375(220), 30);
    self.segControl.backgroundColor = [UIColor clearColor];
    self.segControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segControl.autoresizingMask = UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleRightMargin;
    self.segControl.titleTextAttributes = @{NSForegroundColorAttributeName:RGBACOLOR(255,255,255,0.6),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:16]};
    //设置选项卡被选中的颜色
    self.segControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName:RGB_COLOR(255,255,255),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:16]};
    self.segControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segControl.selectionIndicatorHeight = 0;
    //设置开始时默认选中的下标
    self.segControl.selectedSegmentIndex = 0;
    //设置选中的选项卡的点击事件
    [self.segControl addTarget:self action:@selector(segmentDidChangedValue) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = self.segControl;
    [self setRightButton:[UIImage imageNamed:@"更多。"] title:nil target:self action:@selector(moreClick)];
    
}


-(void)setTableViewUI
{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - 49) style:UITableViewStyleGrouped];
//    UITableViewStylePlain, cell不会跟着区头滑动
//    UITableViewStyleGrouped  cell会跟着区头一起滑动
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

}

-(void)setBottomViewUI
{
    FPBottomButtonView *buttonView = [FPBottomButtonView initBottomButtonViewWithTitle:@"雇佣他"];
    buttonView.threeTextButton.hidden = YES;
//    buttonView.frame = CGRectMake(0, self.view.frame.size.height - 49, SCREEN_WIDTH, 49);
    buttonView.phoneClick = ^{
        FPPublicConmmandVC *publicVC = [[FPPublicConmmandVC alloc] init];
        [self.navigationController pushViewController:publicVC animated:true];
    };
    buttonView.fiveButton.hidden = YES;
    buttonView.sixButton.hidden = YES;
    buttonView.employClick = ^{
        FPPublicConmmandVC *publicVC = [[FPPublicConmmandVC alloc] init];
        [self.navigationController pushViewController:publicVC animated:true];
    };
    
    [self.view addSubview:buttonView];
    if (iPhoneX) {
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.bottom.equalTo(self.view.mas_bottom).offset(-10);
            make.height.equalTo(@49);
        }];
    }else{
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.bottom.equalTo(self.view.mas_bottom);
            make.height.equalTo(@49);
        }];
    }
    
}

#pragma mark - UITableViewDelegate,DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
//        return 2;
        return 0;
    }else{
        return 1;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 48;
    }else if (indexPath.section == 1){
        return 38;
    }else if (indexPath.section == 2){
        //这里判断是否有评论图片，有的话是一种高度，没有的话是另一种高度
        return 160;
//        return 79;
    }else{
        return _webHeight;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 280;
    }else if(section ==1){
        return 90;
    }else if (section == 2){
        return 42;
    }else{
        return 46;
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        FPCommodityHeaderView * sectionView = [FPCommodityHeaderView sectionViewWithHeight:280];
        sectionView.shareImageV.hidden = YES;
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        [sectionView initCommodityDetailDataModel:self.commodityDetailModel.data];
        return sectionView;
    }else if (section ==1){
        DesignSecondSectionHeaderView * sectionView = [DesignSecondSectionHeaderView sectionHeaderViewWithHeight:90];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        [sectionView initCommodityDetailShopModel:self.commodityDetailModel.shop];
        return sectionView;
    }else if (section == 2){
        DesignThirdSectionHeaderView * sectionView = [DesignThirdSectionHeaderView sectionHeaderViewWithHeight:119];
        [sectionView titlesName:@[@"111",@"33333",@"222222",@"33333333",@"111",@"22222222",@"111111111"]];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        return sectionView;
    }
    else{
        DesignFourSectionHeaderView * sectionView = [DesignFourSectionHeaderView sectionHeaderViewWithHeight:46];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        return sectionView;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 59;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        UIView *view = [[UIView alloc] init];
        UIView *bjView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
        bjView.backgroundColor = [UIColor whiteColor];
        [view addSubview:bjView];
        
        UIButton *seeMoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        seeMoreBtn.frame = CGRectMake(141, 10, SCREEN_WIDTH - 282, 24);
        [seeMoreBtn setTitle:@"查看更多" forState:UIControlStateNormal];
        [seeMoreBtn addTarget:self action:@selector(seeMoreClick) forControlEvents:UIControlEventTouchUpInside];
        [seeMoreBtn setTitleColor:RGB_COLOR(237,34,59) forState:UIControlStateNormal];
        seeMoreBtn.titleLabel.font = Kfont(12);
        seeMoreBtn.layer.masksToBounds = YES;
        seeMoreBtn.layer.cornerRadius = 12;
        seeMoreBtn.layer.borderWidth = 0.5;
        seeMoreBtn.layer.borderColor = RGB_COLOR(237,34,59).CGColor;
        [bjView addSubview:seeMoreBtn];
        
        view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
        return view;
    }
    else{
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
        return view;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        FPCommodityDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FPCommodityDetailCell"];
        if (cell == nil) {
            cell = [FPCommodityDetailCell cellWithTableView:tableView andIndexPath:indexPath];
        }
        return cell;
    }else if (indexPath.section == 1){
        FPCommoditySecondCell *commoditySecondCell = [tableView dequeueReusableCellWithIdentifier:@"FPCommoditySecondCell"];
        if (commoditySecondCell == nil) {
            commoditySecondCell = [FPCommoditySecondCell cellWithTableView:tableView andIndexPath:indexPath];
        }
        commoditySecondCell.enterShopClick = ^{
            ShopAdornViewController *shopAdornVC = [[ShopAdornViewController alloc] init];
            shopAdornVC.shop_id = self.commodityDetailModel.data.Id;
            shopAdornVC.isMine = NO;
            [self.navigationController pushViewController:shopAdornVC animated:true];
        };
        return commoditySecondCell;
    }else if(indexPath.section == 2){
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        //这是评论的cell
        FPCommodityThirdCell *commodityThirdCell = [FPCommodityThirdCell cellWithTableView:tableView andIndexPath:indexPath];
        
        return commodityThirdCell;
    }else{
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        FPCommodityFourCell *commodityFourCell = [tableView dequeueReusableCellWithIdentifier:@"FPCommodityFourCell"];
        if (commodityFourCell == nil) {
            commodityFourCell = [FPCommodityFourCell cellWithTableView:tableView andIndexPath:indexPath];
            if (self.commodityDetailArray.count > 0) {
                commodityFourCell.caseDetailModel = self.commodityDetailArray[0];
            }
            commodityFourCell.returnHeight = ^(CGFloat height){
                self.webHeight = height;
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                NSLog(@"%f",height);
            };
            __weak typeof(self) weakSelf = self;
            commodityFourCell.returnHeight = ^(CGFloat height) {
                weakSelf.webHeight = height;
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            };
        }
        
        return commodityFourCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if (indexPath.section == 1) {

        
    }
    
}




@end
